package com.clothes.act;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.clothes.R;
import com.clothes.app.BaseUrl;
import com.clothes.app.ClothesApp;
import com.clothes.http.JsonCallBack;
import com.clothes.mdoel.Worker;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 车工管理信息
 * 添加
 */
public class WorkerMsg extends BaseActivity {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.et_name)
    EditText etName;
    @Bind(R.id.et_no)
    EditText etNo;
    @Bind(R.id.et_tel)
    EditText etTel;
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.et_pwd)
    EditText etPwd;
    @Bind(R.id.et_pwd2)
    EditText etPwd2;

    @Override
    public void initView() {
        setContentView(R.layout.activity_worker_msg);
    }

    @Override
    public void initData() {
        tvTitle.setText("车工号管理");
        mImmersionBar.statusBarColor(R.color.colorPrimaryDark).init();
    }

    @OnClick({R.id.iv_back, R.id.bt_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.bt_login:
                add();
                break;
        }
    }

    private void add() {
        String name = etName.getText().toString().trim();
        String tel = etTel.getText().toString().trim();
        String no = etNo.getText().toString().trim();
        String pwd1 = etPwd.getText().toString().trim();
        String pwd2 = etPwd2.getText().toString().trim();
        if (name.equals("")) {
            showToast("姓名不能为空");
            return;
        }
        if (no.equals("")) {
            showToast("工号不能为空");
            return;
        }
        if (no.length() < 2) {
            showToast("工号至少两位");
            return;
        }
        if (tel.equals("")) {
            showToast("手机号不能为空");
            return;
        }

        if (pwd1.equals("")) {
            showToast("密码不能为空");
            return;
        }

        if(!pwd1.equals(pwd2)){
            showToast("两次输入密码不一致");
            return;
        }

        Worker worker = new Worker();
        worker.setRealName(name);
        worker.setTele(tel);
        worker.setEmplno(no);
        worker.setType("1");
        worker.setPassword(pwd1);
        final String json = new Gson().toJson(worker);
        new Thread(new Runnable() {
            @Override
            public void run() {
                post(json,BaseUrl.Register);
            }
        }).start();

    }

    @Override
    protected void onSuccess(String res) {
        showToast("提交成功");
        onBackPressed();
    }
}
